<?php
class ProblemAction extends Action {
    public function index() {
        init($this);
        if ($this->UserLevel == 0) {
            gohistory();
        }
        else {
            $sql = "";
            if (isset($this->UserId) && $this->UserLevel!=3)
                $sql = " and user.UserId=".$this->UserId;
            $SubId = $this->_get("course");
            if (isset($SubId))
                $sql = $sql."and problem.SubID=".$SubId;
            $this->MenuSelected="Problem";
            $model = new Model();
            $res = $model->table("problem")
                         ->join("subject on subject.SubId=problem.SubId")
                         ->join("`select` on select.SubId=subject.SubId")
                         ->join("user on user.UserId=select.UserId")
                         ->join("answer on answer.ProId=problem.ProId and answer.UserId=user.UserId")
                         ->field("problem.ProId, problem.SubId, subject.SubName, problem.ProDate+problem.ProLength, max(answer.AnsTime)")
                         ->where("problem.ProDate+problem.ProLength>now()".$sql)
                         ->group("problem.ProId")
                         ->order("max(answer.AnsTime) is NULL, problem.ProDate+problem.ProLength asc")
                         ->select();
            foreach ($res as $key => $value) {
                // echo $value["problem.ProDate+problem.ProLength"]."\n";
                $res[$key]["problem.ProDate+problem.ProLength"] = translatetime($value["problem.ProDate+problem.ProLength"]);
                $res[$key]["max(answer.AnsTime)"] = translatetime($value["max(answer.AnsTime)"]);
                // echo $res[$key]["problem.ProDate+problem.ProLength"]."\n";
            }
            $this->ProblemList = $res;
            $this->display();
        }
    }
    public function detail() {
        $select=$this->_get('pid');
        if (isset($select)) {
            init($this);
            $model = new Model();
            if ($this->UserLevel != 2) {
                $sql = "";
                if (isset($this->UserId) && $this->UserLevel!=3)
                    $sql = " and user.UserId=".$this->UserId;
                $res = $model->table("problem")
                             ->join("subject on subject.SubId=problem.SubId")
                             ->join("`select` on select.SubId=subject.SubId")
                             ->join("user on user.UserId=select.UserId")
                             ->join("answer on answer.ProId=problem.ProId and answer.UserId=user.UserId")
                             ->field("subject.SubName, problem.ProDate+problem.ProLength, problem.ProDetail, max(answer.AnsTime)")
                             ->where("problem.ProId=".$select.$sql)
                             ->group("problem.ProId")
                             ->select();
                foreach ($res as $key => $value) {
                    // echo $value["problem.ProDate+problem.ProLength"]."\n";
                    $res[$key]["problem.ProDate+problem.ProLength"] = translatetime($value["problem.ProDate+problem.ProLength"]);
                    $res[$key]["max(answer.AnsTime)"] = translatetime($value["max(answer.AnsTime)"]);
                    // echo $res[$key]["problem.ProDate+problem.ProLength"]."\n";
                }
                $this->pid = $select;
            }
            else {
                $res = $model->table("problem")
                             ->join("subject on subject.SubId=problem.SubId")
                             ->join("`select` on select.SubId=subject.SubId")
                             ->join("answer on answer.ProId=problem.ProId and answer.UserId=user.UserId")
                             ->field("subject.SubName, problem.ProDate+problem.ProLength, problem.ProDetail, count(answer.UserId)")
                             ->where("problem.ProId=".$select)
                             ->group("problem.ProId")
                             ->select();
                foreach ($res as $key => $value) {
                    // echo $value["problem.ProDate+problem.ProLength"]."\n";
                    $res[$key]["problem.ProDate+problem.ProLength"] = translatetime($value["problem.ProDate+problem.ProLength"]);
                    // echo $res[$key]["problem.ProDate+problem.ProLength"]."\n";
                }
                $this->pid = $select;
            }
            $this->detail = $res[0];
            $this->display();
        }
        else {
            echo "<script>window.location.href='javascript:history.go(-1)';</script>";
        }
    }
    public function delproblem() {
        init($this);
        if ($this->UserLevel == 2) {
            // need to add a valid property
            $userid = $this->_get("pid");
            $sql = M('Problem');
            $sql->delete($pid);
        }
        echo "<script>window.location.href='javascript:history.go(-2)';</script>";
    }
    public function addproblem() {
        init($this);
        if ($this->UserLevel != 2) {
            gohistory();
        }
        else {
            $subid = $this->_post("subid");
            $detail = $this->_post("detail");
            $length = $this->_post("length");
            if (isset($subid) && isset($detail) && isset($length)) {
                $data['SubId'] = $subid;
                $data['UserId'] = $this->UserId;
                $data['ProDetail'] = $detail;
                $data['ProDate'] = "now()";
                $data['ProLength'] = $length;
                $tmp = D('Problem');
                $tmp->create($data);
                $tmp->add();
                $this->error = $tmp->getError();
                $model = new Model();
                $res = $model->table("user")
                             ->join("`select` on select.UserId=user.UserId")
                             ->field("user.UserName, user.Email")
                             ->where("user.Level=1 and select.SubId=".$subid)
                             ->select();
                foreach ($res as $key => $value) {
                    // without headers
                    mail($value["Email"],
                         "来自作业信息化系统的邮件",
                         $value["UserName"]."你好,".$this->UserName."老师添加了新的题目，请及时完成");
                }
            }
            $this->MenuSelected = 'AddProblem';
            $model = new Model();
            $res = $model->table("subject")
                         ->join("`select` on select.SubId=subject.SubId")
                         ->join("user on user.UserId=select.UserId")
                         ->field("subject.SubId, subject.SubName")
                         ->where("user.UserId = ".$this->UserId)
                         ->order("subject.SubId")
                         ->select();
            $this->sublist = $res;
            $this->display();
        }
    }
    public function answer() {
        init($this);
        if ($this->UserLevel != 1) {
            // need to be continue;
        }
        else {
            // 文件上传
            import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728;// 设置附件上传大小
            $upload->allowExts  = array();// 设置附件上传类型
            $upload->savePath =  '/Upload/';// 设置附件上传目录
            if(!$upload->upload()) {// 上传错误提示错误信息
                $this->error($upload->getErrorMsg());
            }
            else {
                $info = $upload->getUploadFileInfo();
                $pid = $this->_post("pid");
                $uid = $this->UserId;
                $ans = D('Answer');
                $data["ProId"] = $pid;
                $data["UserId"] = $uid;
                $data["AnsDetail"] = $info["savepath"].$info["name"];
                $data["AnsTime"] = "now()";
                $ans->create($data);
                $ans->add();
            }
        }
        gohistory();
    }
}
?>
